<?php

class map_overlay extends a_admin_controller {
    protected $srv;
    protected $ga;
    protected $getResults;
    protected $region_data;

    protected $numeric_month;
    protected $calendar_month;

    protected $date_array;
    protected $date_range;

    protected $is_region;

    public function __construct() {
        parent::__construct(true);
        $this->srv = new service_l_analytics;
        $this->ga = new stdClass();
        $this->getResults =NULL;
        $this->is_region=1;
        
        $this->numeric_month = array("01","02","03","04","05","06","07","08","09","10","11","12");
        $this->calendar_month = array("January","February","March","April","May","June","July","August","September","October","November","December");

    }

    public function batch_assign(){
        $this->smarty()->assign("ga", $this->ga);
        $this->smarty()->assign("getResults", $this->getResults);
        $this->smarty()->assign("region_data", $this->region_data);


        $this->smarty()->assign("numeric_month", $this->numeric_month);
        $this->smarty()->assign("calender_month", $this->calendar_month);

        $this->smarty()->assign("date_array", $this->date_array);
        $this->smarty()->assign("date_range", $this->date_range);
    }

    public function index()
    {
        $start = trim($this->input->post("start_date"));
        $end = trim($this->input->post("end_date"));

        if($start)
        {
            $this->date_range[0]=$start;
            $month = strtok($start, "/");
            $date = strtok("/");
            $year = strtok("/");

            $start = $year."-".$month."-".$date;
        }
        else
        {
            $start = '2010-12-31';
            $this->date_range[0] = "12/31/2010";
        }
        if($end)
        {
            $this->date_range[1]=$end;
            $month = strtok($end, "/");
            $date = strtok("/");
            $year = strtok("/");

            $end = $year."-".$month."-".$date;
        }
        else
        {
            $end = date("Y-m-d");
            $this->date_range[1] = date("m/d/Y");
        }

        $this->ga_setup($start, $end);
    }

    private function ga_setup($start, $end)
    {
        define('ga_email','ecopoxy@gmail.com');
        define('ga_password','ecopoxysystemscanada');
        define('ga_profile_id','41937276');

        $this->ga = new gapi_services_l_analytics(ga_email, ga_password);

        $this->ga->requestReportData(
                                ga_profile_id,
                                array('day','month','year','country'),
                                array('newVisits','visitors','pageviews','visits','bounces','entrances'),
                                array('-year','-month') ,
//                                array('browser','browserVersion'),
                                $filter=null,
                                $start_date=$start,
                                $end_date=$end,
                                $start_index=1,
                                $max_results=1000
                                );

        $this->getResults = $this->ga->getResults();

        $php_array1 = array();

        foreach($this->ga->getResults() as $result)
        {
            $php_array1[] = array((int)$result->getYear(), (int)$result->getMonth(), (int)$result->getDay(), (int)$result->getVisits(), $result->getPageviews());
        }

        $this->region_data = json_encode($this->srv->get_region_data($this->getResults));

        $this->date_array = json_encode($php_array1);
    }

}

?>